package com.fei.controller;

import com.fei.bean.CommentBean;
import com.fei.bean.PageBean;
import com.fei.bean.UserBean;
import com.fei.bean.VideoBean;
import com.fei.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommentController {

    //用于分页初始化数据
    private PageBean pageBean;

    @Autowired
    CommentService commentService;

//    初始化评论
    @RequestMapping("/user/ajax/videoComment")
    public String videoCommentPage(Integer video_id,Model model,HttpSession session){
        Integer commentCount = commentService.selectOneIdCountService(video_id);
        if (commentCount == 0){
            return "";
        }else{
            VideoBean videoBean = new VideoBean();
            videoBean.setId(video_id);
//            全局变量
            pageBean = new PageBean();
            pageBean.setTotal(commentCount);
            //从哪开始
            pageBean.setNewPage(0);
            //每页数量
            pageBean.setPageSize(5);
            //总数除以每页数量
            int num = commentCount/pageBean.getPageSize();
            int pages = commentCount%pageBean.getPageSize() == 0 ? num : num + 1;
            if (num == 0){
                num++;
            }
            pageBean.setPages(pages);
            pageBean.setVideoBean(videoBean);
            List<CommentBean> commentBeans = commentService.selectOneVideoIdPageService(pageBean);
            model.addAttribute("commentBeans",commentBeans);
            session.setAttribute("pageBean",pageBean);
            session.setAttribute("video_id",video_id);
        }
        return "commons/commons::videoComment";
    }

//    初始化分页组件
    @RequestMapping("/user/ajax/videoCommentPageInit")
    @ResponseBody
    public PageBean videoCommentPageInit(Model model){
        return pageBean;
    }

//      点击每一页时
    @RequestMapping("/user/ajax/videoCommentPage")
    public String videoCommentPage(Integer newPage,Integer limit,HttpSession session,Model model){
        Integer video_id = (Integer) session.getAttribute("video_id");
        VideoBean videoBean = new VideoBean();
        videoBean.setId(video_id);
        PageBean pageBean = new PageBean();
        pageBean.setNewPage(newPage);
        pageBean.setPageSize(limit);
        pageBean.setVideoBean(videoBean);
        List<CommentBean> commentBeans = commentService.selectOneVideoIdPageService(pageBean);
        model.addAttribute("commentBeans",commentBeans);
        return "commons/commons::videoComment";
    }

    //用户评论视频
    @RequestMapping("/user/ajax/videoCommentInsert")
    public String videoComment(String text, Integer id, HttpSession session, Model model){
        UserBean user = (UserBean)session.getAttribute("user");
        CommentBean commentBean = new CommentBean();
        commentBean.setPhone(user.getPhone());
        commentBean.setText(text);
        commentBean.setVideo_id(id);
        commentService.insertVideoCommentService(commentBean);
        Integer commentCount = commentService.selectOneIdCountService(id);
        int num = commentCount/pageBean.getPageSize();
        num = commentCount%pageBean.getPageSize() == 0 ? num : num + 1;
        if (num == 0){
            num++;
        }
        pageBean.setTotal(pageBean.getTotal() + 1);
        pageBean.setPages(num);
        return "redirect:/user/ajax/videoCommentPageInit";
    }


}
